package cn.jsj.hzu.qm.facecloud.ciuser.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CiImageServlet
 */
public class CiImageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String myImgPathString = request.getParameter("myImgPath");
		if (myImgPathString == null) {
			System.out.println("MMP");
			return;
		}
		/**
		 * 图片路径<br/>
		 * 为了防止换项目图片没有了，此处可以设为项目之外的图片路径，也可以自动获取项目内的图片路径<br/>
		 * <b>默认为项目内的相对路径</b>
		 * */
		String baseImgPathString;
		
		//项目图片文件夹相对路径
		baseImgPathString = this.getServletContext().getRealPath("ciface");

		myImgPathString = baseImgPathString + File.separatorChar + myImgPathString;
		File file = new File(myImgPathString);

		//如果请求的图片不存在错误，返回默认错误图片
		if (file.isFile() == false) {
			file = new File(baseImgPathString + File.separatorChar + "ciuser_noimg.jpg");
		}

		FileInputStream fileInputStream = new FileInputStream(file);

		OutputStream outputStream = response.getOutputStream();

		int b;

		while ((b = fileInputStream.read()) != -1) {
			outputStream.write(b);
		}

		fileInputStream.close();
		outputStream.close();
		return;
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
